/*
 * Created by JFormDesigner on Fri Apr 13 19:28:01 PDT 2012
 */

package org.cloudbus.cloudsim.examples.power.parallel.helper;

import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

import javax.swing.*;

import org.cloudbus.cloudsim.examples.power.Helper;
import org.cloudbus.cloudsim.examples.power.parallel.MainFieldInfo;
import org.jfree.ui.RefineryUtilities;

/**
 * @author hoai nam nguyen
 */
@SuppressWarnings("serial")
public class SummayTable extends JFrame {

	private List<MainFieldInfo> list;
	private JobsPerYear perYear;
	private JobsPerDay perDay;
	private JobsPerHour perHour;
	DecimalFormat df = new DecimalFormat("#.##");
	private HashMap<String, RuntimeInfo> hashMap;

	public SummayTable(List<MainFieldInfo> mainFieldInfos) {
		initComponents();
		pack();
		list = mainFieldInfos;
		perYear = new JobsPerYear(list);
		perDay = new JobsPerDay(list);
		perHour = new JobsPerHour(list);
		hashMap = Helper.setHashmapValue(list);
		CalcAvgField();
		CreateRuntimeData();
	}

	private void CalcAvgField() {
		txtNumberYear.setText(String.format("%d %s", perYear.getNumberYear(),
				"Years"));
		txtNumberHour.setText(String.format("%d %s", perHour.getNumberHour(),
				"Hours"));
		txtNumberDay.setText(String.format("%d %s", perDay.getNumberDay(),
				"Days"));
		txtSYear.setText(df.format(perYear.calAverageJobPerTime()));
		txtSDay.setText(df.format(perDay.calAverageJobPerTime()));
		txtSHour.setText(df.format(perHour.calAverageJobPerTime()));
	}

	private void CreateRuntimeData() {
		StringBuilder builder = new StringBuilder();
		int i = 0, j = 1;
		while (j < hashMap.size()) {
			int numberJob = hashMap.get(String.format("%d%d", i, j))
					.getJobValue();
			String percentJob = hashMap.get(String.format("%d%d", i, j))
					.getJpbPercent();
			builder.append(String
					.format("Runtime from %d to %d days has %d jobs, proportion of %s percent",
							i, j, numberJob, percentJob));
			builder.append("\n");
			i++;
			j++;
		}
		builder.append(String
				.format("Runtime undefined has %d jobs, proportion of %s percent",
						hashMap.get("Undefined").getJobValue(), hashMap.get("Undefined").getJpbPercent()));
		builder.append("\n");
		textPaneRuntime.setText(builder.toString());
	}

	private void btnSYearActionPerformed(ActionEvent e) {
		// TODO add your code here
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				DistributeJobChart chartYear = new DistributeJobChart(
						"Detail of Distribution Year",
						perYear.getClusterJobs(), "Year",
						"Amount Of Submitted Job Per Year");

				chartYear.pack();
				RefineryUtilities.centerFrameOnScreen(chartYear);
				chartYear.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
				chartYear.setVisible(true);

			}
		});
	}

	private void btnSDayActionPerformed(ActionEvent e) {
		// TODO add your code here
		DistributeJobChart chartDay = new DistributeJobChart(
				"Detail of Distribution Day", perDay.getClusterJobs(), "Day",
				"Amount Of Submitted Job Per Day");

		chartDay.pack();
		RefineryUtilities.centerFrameOnScreen(chartDay);
		chartDay.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		chartDay.setVisible(true);
	}

	private void btnSHourActionPerformed(ActionEvent e) {
		// TODO add your code here
		DistributeJobChart chartHour = new DistributeJobChart(
				"Detail of Distribution Hour", perHour.getClusterJobs(),
				"Hour", "Amount Of Submitted Job Per Hour");

		chartHour.pack();
		chartHour.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		RefineryUtilities.centerFrameOnScreen(chartHour);
		chartHour.setVisible(true);
	}

	private void btnRViewActionPerformed(ActionEvent e) {
		// TODO add your code here
		// System.out.println(runtime.getJobBaseValue(3600,7200));

		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				RuntimeChart demo = new RuntimeChart("Statistical Report",
						hashMap);
				demo.pack();
				RefineryUtilities.centerFrameOnScreen(demo);
				demo.setVisible(true);
				demo.setForeground(Color.black);
			}
		});

	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - hoai nam nguyen
		label1 = new JLabel();
		label2 = new JLabel();
		label3 = new JLabel();
		txtSYear = new JTextField();
		txtSDay = new JTextField();
		label4 = new JLabel();
		txtSHour = new JTextField();
		label5 = new JLabel();
		label6 = new JLabel();
		label7 = new JLabel();
		btnSYear = new JButton();
		btnSDay = new JButton();
		btnSHour = new JButton();
		txtNumberYear = new JTextField();
		txtNumberDay = new JTextField();
		txtNumberHour = new JTextField();
		label8 = new JLabel();
		label9 = new JLabel();
		separator1 = new JSeparator();
		separator2 = new JSeparator();
		btnRView = new JButton();
		scrollPane1 = new JScrollPane();
		textPaneRuntime = new JTextPane();

		//======== this ========
		setTitle("Summary Report");
		setMinimumSize(new Dimension(580, 485));
		setFont(new Font("Times New Roman", Font.PLAIN, 12));
		setResizable(false);
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		//---- label1 ----
		label1.setText("Avg submitted job per year");
		contentPane.add(label1);
		label1.setBounds(new Rectangle(new Point(10, 80), label1.getPreferredSize()));

		//---- label2 ----
		label2.setText("Avg submitted job per day");
		contentPane.add(label2);
		label2.setBounds(new Rectangle(new Point(10, 115), label2.getPreferredSize()));

		//---- label3 ----
		label3.setText("Avg submitted job per hour");
		contentPane.add(label3);
		label3.setBounds(new Rectangle(new Point(10, 150), label3.getPreferredSize()));

		//---- txtSYear ----
		txtSYear.setFont(new Font("Tahoma", Font.BOLD, 11));
		txtSYear.setForeground(Color.blue);
		contentPane.add(txtSYear);
		txtSYear.setBounds(265, 80, 85, txtSYear.getPreferredSize().height);

		//---- txtSDay ----
		txtSDay.setFont(new Font("Tahoma", Font.BOLD, 11));
		txtSDay.setForeground(Color.blue);
		contentPane.add(txtSDay);
		txtSDay.setBounds(265, 115, 85, txtSDay.getPreferredSize().height);

		//---- label4 ----
		label4.setText("Summary Of Average Distribution Of Job Based On Submit Time");
		label4.setFont(new Font("Tahoma", Font.BOLD, 14));
		label4.setVerticalAlignment(SwingConstants.TOP);
		contentPane.add(label4);
		label4.setBounds(new Rectangle(new Point(55, 15), label4.getPreferredSize()));

		//---- txtSHour ----
		txtSHour.setFont(new Font("Tahoma", Font.BOLD, 11));
		txtSHour.setForeground(Color.blue);
		contentPane.add(txtSHour);
		txtSHour.setBounds(265, 150, 85, txtSHour.getPreferredSize().height);

		//---- label5 ----
		label5.setText("jobs/year");
		contentPane.add(label5);
		label5.setBounds(new Rectangle(new Point(360, 80), label5.getPreferredSize()));

		//---- label6 ----
		label6.setText("jobs/day");
		contentPane.add(label6);
		label6.setBounds(new Rectangle(new Point(360, 120), label6.getPreferredSize()));

		//---- label7 ----
		label7.setText("jobs/hour");
		contentPane.add(label7);
		label7.setBounds(new Rectangle(new Point(360, 155), label7.getPreferredSize()));

		//---- btnSYear ----
		btnSYear.setText("View Detail...");
		btnSYear.setMinimumSize(new Dimension(150, 23));
		btnSYear.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				btnSYearActionPerformed(e);
			}
		});
		contentPane.add(btnSYear);
		btnSYear.setBounds(420, 80, 120, btnSYear.getPreferredSize().height);

		//---- btnSDay ----
		btnSDay.setText("View Detail...");
		btnSDay.setMinimumSize(new Dimension(150, 23));
		btnSDay.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				btnSDayActionPerformed(e);
			}
		});
		contentPane.add(btnSDay);
		btnSDay.setBounds(420, 115, 120, 23);

		//---- btnSHour ----
		btnSHour.setText("View Detail...");
		btnSHour.setMinimumSize(new Dimension(150, 23));
		btnSHour.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				btnSHourActionPerformed(e);
			}
		});
		contentPane.add(btnSHour);
		btnSHour.setBounds(420, 150, 120, 23);

		//---- txtNumberYear ----
		txtNumberYear.setForeground(Color.blue);
		txtNumberYear.setFont(new Font("Tahoma", Font.BOLD, 11));
		contentPane.add(txtNumberYear);
		txtNumberYear.setBounds(170, 80, 85, txtNumberYear.getPreferredSize().height);

		//---- txtNumberDay ----
		txtNumberDay.setForeground(Color.blue);
		txtNumberDay.setFont(new Font("Tahoma", Font.BOLD, 11));
		contentPane.add(txtNumberDay);
		txtNumberDay.setBounds(170, 115, 85, 20);

		//---- txtNumberHour ----
		txtNumberHour.setForeground(Color.blue);
		txtNumberHour.setFont(new Font("Tahoma", Font.BOLD, 11));
		contentPane.add(txtNumberHour);
		txtNumberHour.setBounds(170, 150, 85, 20);

		//---- label8 ----
		label8.setText("This Workload Has 243314 Jobs");
		label8.setFont(new Font("Tahoma", Font.BOLD, 12));
		label8.setForeground(Color.red);
		contentPane.add(label8);
		label8.setBounds(new Rectangle(new Point(175, 40), label8.getPreferredSize()));

		//---- label9 ----
		label9.setFont(new Font("Tahoma", Font.BOLD, 14));
		label9.setVerticalAlignment(SwingConstants.TOP);
		label9.setText("Statistical Report Of Job Based On Runtime");
		contentPane.add(label9);
		label9.setBounds(130, 200, 320, 17);
		contentPane.add(separator1);
		separator1.setBounds(10, 180, separator1.getPreferredSize().width, 2);
		contentPane.add(separator2);
		separator2.setBounds(new Rectangle(new Point(105, 320), separator2.getPreferredSize()));

		//---- btnRView ----
		btnRView.setText("View Chart...");
		btnRView.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnRView.setForeground(Color.blue);
		btnRView.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				btnRViewActionPerformed(e);
			}
		});
		contentPane.add(btnRView);
		btnRView.setBounds(205, 415, 155, btnRView.getPreferredSize().height);

		//======== scrollPane1 ========
		{

			//---- textPaneRuntime ----
			textPaneRuntime.setFont(new Font("Times New Roman", Font.PLAIN, 14));
			scrollPane1.setViewportView(textPaneRuntime);
		}
		contentPane.add(scrollPane1);
		scrollPane1.setBounds(40, 225, 480, 170);

		{ // compute preferred size
			Dimension preferredSize = new Dimension();
			for(int i = 0; i < contentPane.getComponentCount(); i++) {
				Rectangle bounds = contentPane.getComponent(i).getBounds();
				preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
				preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
			}
			Insets insets = contentPane.getInsets();
			preferredSize.width += insets.right;
			preferredSize.height += insets.bottom;
			contentPane.setMinimumSize(preferredSize);
			contentPane.setPreferredSize(preferredSize);
		}
		pack();
		setLocationRelativeTo(getOwner());
		// //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY
	// //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - hoai nam nguyen
	private JLabel label1;
	private JLabel label2;
	private JLabel label3;
	private JTextField txtSYear;
	private JTextField txtSDay;
	private JLabel label4;
	private JTextField txtSHour;
	private JLabel label5;
	private JLabel label6;
	private JLabel label7;
	private JButton btnSYear;
	private JButton btnSDay;
	private JButton btnSHour;
	private JTextField txtNumberYear;
	private JTextField txtNumberDay;
	private JTextField txtNumberHour;
	private JLabel label8;
	private JLabel label9;
	private JSeparator separator1;
	private JSeparator separator2;
	private JButton btnRView;
	private JScrollPane scrollPane1;
	private JTextPane textPaneRuntime;
	// JFormDesigner - End of variables declaration //GEN-END:variables
}
